package vn.com.shoppie.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	Gallery glrShopIcon;
	
	Integer pics[] = { R.drawable.icon_shop, R.drawable.icon_shop, R.drawable.icon_shop, R.drawable.icon_shop,
			R.drawable.icon_shop };
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        glrShopIcon=(Gallery)findViewById(R.id.glr_icon_shop);
        glrShopIcon.setAdapter(new ImageAdapter(this));
        
        glrShopIcon.setSelection(pics.length/2, true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    class GlrAdapter extends ArrayAdapter<Integer>{
    	LayoutInflater inflater;
    	Integer pics[];
    	int imageBackground;
		public GlrAdapter(Context context, int textViewResourceId,
				Integer[] objects) {
			super(context, textViewResourceId, objects);
			pics=objects;
			
			inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
			imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
			ta.recycle();
		}
		@Override
    	public int getCount() {
    		
    		return pics.length;
    	}

    	@Override
    	public Integer getItem(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public long getItemId(int arg0) {
    		
    		return arg0;
    	}
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v=convertView;
    		if(v==null){
    			ItemShopHolder holder=new ItemShopHolder();
    			v=inflater.inflate(R.layout.item_shop, parent,false);
    			holder.avatar=(ImageView)v.findViewById(R.id.iv_avatar);
    			holder.avatar.setScaleType(ImageView.ScaleType.FIT_XY);
    			holder.avatar.setLayoutParams(new Gallery.LayoutParams(150,120));
    			holder.avatar.setBackgroundResource(imageBackground);
    			holder.title=(TextView)v.findViewById(R.id.tv_title);
    			holder.desc=(TextView)v.findViewById(R.id.tv_desc);
    			
    			v.setTag(holder);
    		}
    		ItemShopHolder holder=(ItemShopHolder)v.getTag();
    		holder.avatar.setImageResource(pics[position]);
			return v;
    	}
    }
    class ItemShopHolder{
    	LinearLayout lyAvatar;
    	ImageView avatar;
    	TextView title;
    	TextView desc;
    }
    
    public class ImageAdapter extends BaseAdapter {
    	LayoutInflater inflater;
    	private Context ctx;
    	int imageBackground;
    	
    	public ImageAdapter(Context c) {
			ctx = c;
			inflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
			imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
			ta.recycle();
		}

		@Override
    	public int getCount() {
    		
    		return pics.length;
    	}

    	@Override
    	public Object getItem(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public long getItemId(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v=convertView;
    		if(v==null){
    			ItemShopHolder holder=new ItemShopHolder();
    			v=LayoutInflater.from(ctx).inflate(R.layout.item_shop, parent,false);
    			holder.lyAvatar=(LinearLayout)v.findViewById(R.id.ly_avatar);
    			
    			holder.avatar = new ImageView(ctx);
    			holder.avatar.setImageResource(pics[position]);
    			holder.avatar.setScaleType(ImageView.ScaleType.FIT_XY);
    			holder.avatar.setLayoutParams(new Gallery.LayoutParams(250,250));
//    			holder.avatar.setBackgroundResource(imageBackground);
        		holder.lyAvatar.addView(holder.avatar);
        		
    			holder.title=(TextView)v.findViewById(R.id.tv_title);
    			holder.desc=(TextView)v.findViewById(R.id.tv_desc);
    			
    			v.setTag(holder);
    		}
    		ItemShopHolder holder=(ItemShopHolder)v.getTag();
    		holder.avatar.setImageResource(pics[position]);
    		return v;
    	}

    }
}
